﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Astor.UI.Framework.ViewModels;
using Astor.Modules.DataSources.Models;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using Astor.Modules.DataSources.Services;
using System.ComponentModel;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;

namespace Astor.Modules.DataSources.Screens.Details
{
    public class DataSourceDetailsViewModel : ConfirmationViewModel
    {
        private IDataSourcesService _dataSourcesService;
        private bool _isEdit;
        private string _originalDataSourceName;
        private InteractionRequest<Notification> _closeIterationRequest = new InteractionRequest<Notification>();

        public DataSourceDetailsViewModel(IDataSourcesService dataSourcesService)
        {
            _dataSourcesService = dataSourcesService;

            SubmitCommand = new DelegateCommand(OnSubmit, () => CanSubmit);
        }

        public void InitializeAdd(string title)
        {
            _isEdit = false;
            Title = title;
            DataSource = new DataSource();
            AssignEventHandlers();
        }

        public void InitializeEdit(string title, DataSource ds)
        {
            _isEdit = true;
            Title = title;
            DataSource = ds;
            _originalDataSourceName = DataSource.Name;
            AssignEventHandlers();
        }

        private void AssignEventHandlers()
        {
            DataSource.ErrorsChanged += new EventHandler<DataErrorsChangedEventArgs>(DataSource_ErrorsChanged);
            DataSource.PropertyChanged += new PropertyChangedEventHandler(DataSource_PropertyChanged);
        }

        void DataSource_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            DataSource ds = (DataSource)sender;
            if (e.PropertyName == ds.PropertyName(() => ds.Name))
            {
                if (!_isEdit || ds.Name != _originalDataSourceName)
                {
                    bool exists = _dataSourcesService.Exists(ds.Name);
                    if (exists) ds.AddError(DataSource.PropertyNames.Name, "Name already exists");
                }
            }
        }

        void DataSource_ErrorsChanged(object sender, System.ComponentModel.DataErrorsChangedEventArgs e)
        {
            RaisePropertyChanged(() => CanSubmit); 
        }

        public DataSource DataSource { get; private set; }

        public bool CanSubmit
        {
            get
            {
                return !DataSource.HasErrors;
            }
        }

        public void Validate()
        {
            DataSource.ValidateObject();
        }

        public DelegateCommand SubmitCommand { get; private set; }

        public void OnSubmit()
        {
            DataSource.ValidateObject();
            if (CanSubmit)
            {
                Confirmed = true;
                _closeIterationRequest.Raise(null);
            }
        }

        public IInteractionRequest CloseRequest
        {
            get { return _closeIterationRequest; }
        }
    }
}
